import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import '../common/flatPanel.dart';
import '../controller/dailyController.dart';
import '../entry.dart';
import '../utils/screw.dart';

class DailyPage extends StatefulWidget {
  DailyPage(
      {Key key,
      @required this.title,
      @required this.table,
      @required this.configName})
      : super();

  final String title;
  final String table;
  final String configName;

  @override
  _DailyPageState createState() => _DailyPageState();
}

class _DailyPageState extends State<DailyPage> with DailyController {
  @override
  void initState() {
    super.initState();
    mTable = widget.table;
    mMonths.add(Screw.genMonthString());
    mDate = Screw.genDateString();
  }

  @override
  void dispose() {
    super.dispose();
    disposeAction();
  }

  @override
  Future<void> onTapItem() async {
    if (mItem.id == placeHolder) {
      mViewState = ViewState.DetailView;
      placeHolder = -1;
    } else {
      mViewState = ViewState.CheckedView;
      placeHolder = mItem.id;
    }
    setState(() {});
  }

  Future<void> _onPaste() async {
    if (mItem.id == placeHolder) {
      await Clipboard.setData(ClipboardData(text: mItem.detail));
      Screw.popup(context, '${mItem.category} 已复制到剪贴板');
    }
  }

  Future<void> onConnectDb() async {
    connectDbAction(context, widget.configName, onUpdateMonths);
  }

  Future<void> onUpdateMonths() async {
    await updateMonthsAction();
    setState(() {
      mMonth = mMonths.first;
      mViewState = ViewState.TopicView;
    });
  }

  Future<void> onUpdateDates() async {
    await updateDatesAction();
    setState(() {
      mViewState = ViewState.DetailView;
    });
  }

  @override
  Future<void> onUpdateView() async {
    await updateViewAction();
    setState(() {});
  }

  @override
  Future<void> onAddItem() async {
    await addItemAction(context);
  }

  @override
  Future<void> onEditItem() async {
    await editItemAction(context);
  }

  Widget _genDropDownButton() {
    return DropdownButton<String>(
      isExpanded: true,
      hint: Text('月份'),
      value: mMonth,
      items: mMonths
          .map((e) => DropdownMenuItem<String>(
                value: e,
                child: Text(e),
              ))
          .toList(),
      onChanged: (v) {
        mMonth = v;
        onUpdateDates();
      },
    );
  }

  Widget _updateFloatingActionButton() {
    switch (mViewState) {
      case ViewState.Initial:
        return FloatingActionButton(
          child: FaIcon(FontAwesomeIcons.plug),
          onPressed: onConnectDb,
        );
      case ViewState.TopicView:
        return FloatingActionButton(
          child: Icon(Icons.read_more),
          onPressed: onUpdateDates,
        );
      case ViewState.CheckedView:
        return FloatingActionButton(
          child: Icon(Icons.edit),
          onPressed: onEditItem,
        );
      default:
        return FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: onAddItem,
        );
    }
  }

  PopupMenuButton<String> _genPopupMenuButton() {
    return PopupMenuButton<String>(
      offset: Offset(0, 60),
      itemBuilder: (context) {
        return <PopupMenuEntry<String>>[
          PopupMenuItem<String>(
            value: 'updateMonths',
            child: Text('更新月份列表'),
          ),
          PopupMenuItem<String>(
            value: 'updateDates',
            child: Text('更新日期列表'),
          ),
        ];
      },
      onSelected: (value) {
        if (value == 'updateMonths')
          onUpdateMonths();
        else if (value == 'updateDates') updateDatesAction();
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.refresh),
            tooltip: '刷新视图',
            onPressed: onUpdateView,
          ),
          _genPopupMenuButton(),
          IconButton(
            icon: Icon(Icons.copy),
            tooltip: '复制',
            onPressed: _onPaste,
          ),
          IconButton(
            icon: Icon(Icons.settings),
            tooltip: '数据源编辑器',
            onPressed: () => Navigator.pushNamed(context, Routes.DSEditor),
          ),
        ],
      ),
      body: FlatPanel(
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  _genDropDownButton(),
                  Expanded(
                    child: genDatesViewAction(),
                  ),
                ],
              ),
            ),
            Expanded(
              flex: 4,
              child: genDetailViewAction(),
            ),
          ],
        ),
      ),
      floatingActionButton: _updateFloatingActionButton(),
    );
  }
}
